/**	
 * DroidKit is a toolkit for the Android platform
 * Copyright (C) 2012  Jonathan Bonazza
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

package com.droidkit.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * API for communicating with PHP server scripts over the HTTP protocol.
 * 
 * Project home page:  http://code.google.com/p/droid-kit/
 * Author: Jonathan Bonazza
 * License: GPL
 */
public class PhpClient extends DefaultHttpClient
{
	protected String url;		//url for the connection (not including form entity)
	
	//Default Ctor
	public PhpClient() { }
	//Initializing Ctor
	public PhpClient(String url) {  this.url = url; }
	
	/*
	 * @brief: execute a POST request
	 * @param: a list of name-value pairs representing the form entity
	 * @return: the response from the server
	 */
	public HttpResponse executePost(List<NameValuePair> nvp) throws IOException
	{	
		HttpPost post = new HttpPost(url);
		post.setEntity(new UrlEncodedFormEntity(nvp));
		
		return execute(post);
	}
	
	/*
	 * @brief: execute a GET request
	 * @param: void
	 * @return: the response from the server
	 */
	public HttpResponse executeGet() throws IOException
	{	
		HttpGet get = new HttpGet(url);
		return execute(get);
	}
	
	/*
	 * @brief: utility method to convert a response object to an array of JSON objects
	 * @param: the response object to convert
	 * @return: a new array of JSON objects
	 */
	public static JSONArray responseToJSON(HttpResponse response) throws IOException, JSONException
	{
		HttpEntity entity = response.getEntity();
		
		InputStream is = entity.getContent();
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
		String result = "";
		String line = null;
		while((line = reader.readLine()) != null)
			result += line + "\n";
		
		is.close();
		
		return new JSONArray(result);
	}
	
	@Override
	public String toString()
	{
		return url;
	}
	
	//Accessor and mutator methods
	public String getUrl()
	{
		return url;
	}
	
	public void setUrl(String url)
	{
		this.url = url;
	}
}
